<?php
include_once('../lib/config.php');
include_once('../lib/Db.class.php');
include_once('../lib/Model.class.php');
include_once('../lib/Snoopy.class.php');
include_once('../lib/function.php');
include_once('/usr/local/xunsearch/sdk/php/lib/XS.php');

// http://book.gouyg.com/index.php 搜索词入队列
// BOOK_SEARCH_QUEUE, from github.bookserch, 1、index_search.php
// BOOK_CHAPTER_QUEUE, fetch chapters list, 2、search_chapter.php
// BOOK_CONTENT_QUEUE, fetch chapter content，3、chapter_fetch.php

$redis = new Redis();
$ret = $redis->connect("localhost","6379");  //php客户端设置的ip及端口
if(!$ret) die('redis connction faild');
$redis->auth(REDIS_PASS);
$redis->select(2);

$use_proxy = true;
$limit = 100;
while ($limit--) {
    try {
        $q = $redis->lPop('BOOK_SEARCH_QUEUE');
        if(!$q) {
            //echo2('BOOK_SEARCH_QUEUE empty, limit:'.$limit);
            sleep(2);
            continue;
        }

        //判断是否有搜索过
        $is_searched = $redis->hGet('book_search', md5($q));
        if($is_searched || strlen($q) > 40) {
            echo2('BOOK_SEARCHED, '.$q);
            continue;
        }

        $xs = new XS('booksonline');
        $search = $xs->search;
        $index  = $xs->index;

        $url = 'http://www.woquge.com/modules/article/search.php?searchkey='.urlencode($q);
        $snoopy = new Snoopy;
        $snoopy->curl($url, $use_proxy);
        $ori_result = iconv('GBK', 'utf-8//IGNORE', $snoopy->results);
        $result = cut('<table class="grid"', '</table>', $ori_result);
        preg_match_all('/<tr>(.*?)<\/tr>/is', $result, $matches);

        if($snoopy->status == 0) {
            $use_proxy = !$use_proxy;
        }
        echo2('q:' . $q . ', '.$url.', status:'.$snoopy->status.', use_proxy:'.intval($use_proxy));
        $titles = [];
        if(count($matches[0]) > 0) {
            foreach ($matches[0] as $item) {
                preg_match_all('/<td[^>]*>(.*?)<\/td>/i', $item, $matches2);
                $match2 = $matches2[1];

                //获取链接标题
                preg_match('/<a href="(.*)"[^>]*>(.*)<\/a>/i', $match2[0], $match3);
                preg_match('/<a href="([^"]*)"[^>]*>(.*)<\/a>/i', $match2[1], $match4);

                $index_data = array(
                    'md5id' => md5($match3[1]),
                    'url' => $match3[1],
                    'title' => trim($match3[2]),
                    'last_chapter_url' => $match4[1],
                    'last_chapter' => $match4[2],
                    'author' => $match2[2],
                    'size' => $match2[3],
                    'updatetime' => strtotime($match2[4]),
                    'state' => $match2[5],
                    'indextime' => time(), //索引添加时间
                );

                //有同标题则跳过
                if(in_array($index_data['title'], $titles)) {
                    echo2('title repeat:'.$index_data['title']);
                    continue;
                } else {
                    $titles[] = $index_data['title'];
                }

                //7天内有更新则放入连载队列
                if($index_data['updatetime'] >= time() - 3600*24*7) {
                    $redis->hSet('book_ongoing', $index_data['md5id'], json_encode($index_data));
                }

                //检查是否有更新
                $chapters_list = $redis->get('books:'.$index_data['md5id']);
                if($chapters_list) {
                    $chapters_list = json_decode($chapters_list, true);
                    if($chapters_list['last_chapter'] == $index_data['last_chapter']) {
                        echo2('章节没有更新, title:'.$index_data['title']);
                        continue;
                    }

                    if(isset($chapters_list['urls'])) {
                        unset($chapters_list['urls'], $chapters_list['titles']);
                        $redis->set('books:'.$index_data['md5id'], json_encode($chapters_list));
                    }
                }

                // 创建文档对象
                $doc = new XSDocument;
                $doc->setFields($index_data);

                // 更新到索引数据库中
                $index->update($doc);

                echo2($index_data['title'].', Total:'.$search->getDbTotal());
                $redis->rPush('BOOK_CHAPTER_QUEUE', json_encode($index_data));
            }
        } else {
            file_put_contents('./read_all/search/'.urlencode($url), $ori_result);
            if($snoopy->status != 200) {
                throw new Exception('response_code:'.$snoopy->response_code);
            }
        }

        $redis->hSet('book_search', md5($q), $q);

    } catch (Exception $e) {
        echo2($e->getMessage());

        if($q) {
            $redis->rPush('BOOK_SEARCH_QUEUE', $q);
            sleep(1);
        }
    }

    echo2('sesrch count:'.count($matches[0]));
}

echo2('BOOK_SEARCH_QUEUE empty, limit:'.$limit);
